//
//  NRWCarConfInfoVC.m
//  NRWMall
//
//  Created by 张记 on 2018/5/24.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWCarConfInfoVC.h"
#import "CarConfigHeaferView.h"
#import "NRWCarConfCell.h"
#import "NRWCarBaseConfigInfo.h"
#import "NRWCarConfigInfoModel.h"
#import "NRWCarConfigInfoApi.h"
@interface NRWCarConfInfoVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property(nonatomic, assign) BOOL selectedState;
@property(nonatomic, strong) UILabel *mainLabel;
@property(nonatomic, strong) NSMutableArray *carConfigArray;
@property(nonatomic, strong) NRWCarConfigInfoApi *configInfoApi;
@end

@implementation NRWCarConfInfoVC
-(NSMutableArray *)carConfigArray{
    if (!_carConfigArray) {
        _carConfigArray = [[NSMutableArray alloc] init];
    }
    return _carConfigArray;
}
-(NRWCarConfigInfoApi *)configInfoApi{
    if (!_configInfoApi) {
        _configInfoApi = [[NRWCarConfigInfoApi alloc] init];
        _configInfoApi.delegate = self;
    }
    return _configInfoApi;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationView setTitle:@"参数配置"];
    self.selectedState = YES;
    [self initTableview];
    self.mainLabel.text = self.detailModel.result.carTitle;
    [self loadCarBaseInfo];

}
-(void)loadCarBaseInfo{
 
    NSArray *baseInfoArray =  [NRWCarBaseConfigInfo getCarBaseConfigInfo:self.detailModel];
    for (NSDictionary *dic in baseInfoArray) {
        CarConfigResult *child = [CarConfigResult mj_objectWithKeyValues:dic];
        [self.carConfigArray addObject:child];
    }
    [SVProgressHUD show];
    self.configInfoApi.carId = self.detailModel.result.carId;
    [self.configInfoApi start];
}
-(UILabel *)mainLabel{
    if (!_mainLabel) {
        _mainLabel = [[UILabel alloc] init];
        _mainLabel.font = [UIFont systemFontOfSize:16];
        _mainLabel.textColor = mainFontColor();
        _mainLabel.numberOfLines = 0 ;
        [self.view addSubview:_mainLabel];
        kSelfWeak;
        [_mainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view).mas_equalTo(16);
            make.top.equalTo(weakSelf.view).mas_offset(getNavHeight());
            make.right.equalTo(weakSelf.view).mas_equalTo(-16);
            make.height.mas_equalTo(60);

        }];
    }
    return _mainLabel;
}

-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[CarConfigHeaferView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableview registerNib:[UINib nibWithNibName:@"NRWCarConfCell" bundle:nil] forCellReuseIdentifier:@"NRWCarConfCell"];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(getNavHeight() + 60);
        make.left.right.bottom.equalTo(weakSelf.view);
    }];
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.carConfigArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    CarConfigResult *info = self.carConfigArray[section];
    
    if (info.selectedState == NO) {
        return 0;
    }else{
        
        return info.childCarConf.count;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CarConfigResult *info = self.carConfigArray[indexPath.section];
    Childcarconf *childInfo = info.childCarConf[indexPath.row];
    NRWCarConfCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCarConfCell"];
    cell.index = indexPath;
    cell.carConfig = childInfo;
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CarConfigResult *info = self.carConfigArray[section];
    
    CarConfigHeaferView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    view.label.text = info.confName;
    view.rightImage.image = [UIImage imageNamed:@"down"];
    if (info.selectedState == YES) {
        CGAffineTransform transform= CGAffineTransformMakeRotation(M_PI * 2);
        view.rightImage.transform = transform;
    }else{
        CGAffineTransform transform= CGAffineTransformMakeRotation(M_PI);
        view.rightImage.transform = transform;
    }
    view.tag = section;
    if (view.gestureRecognizers == nil) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerViewClickedAction:)];
        [view addGestureRecognizer:tap];
    }
    
    return view;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void) headerViewClickedAction:(UITapGestureRecognizer *)sender
{
    CarConfigResult *info = self.carConfigArray[sender.view.tag];
    if (info.selectedState == YES) {
        info.selectedState = NO;
    }else{
        info.selectedState = YES;
    }
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:sender.view.tag];
    [self.tableview reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 31;
}

#pragma mark 数据请求
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    NRWCarConfigInfoModel *configInfo = [NRWCarConfigInfoModel mj_objectWithKeyValues:request.responseObject];
    
    for (NSDictionary *dic in configInfo.result) {
        CarConfigResult *child = [CarConfigResult mj_objectWithKeyValues:dic];
        child.selectedState = YES;
        [self.carConfigArray addObject:child];
    }
    
    [self.tableview reloadData];
    [SVProgressHUD dismiss];
}
- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    [SVProgressHUD dismiss];
    [NRWMessageHUD showMessage:@"请求失败" forTime:2.0 withRemindId:remindFail];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
